package com.yjc.drygoods.ware.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yjc.drygoods.ware.vo.ExMergeVo;
import com.yjc.drygoods.ware.vo.ExchangeDoneVo;
import com.yjc.drygoods.ware.vo.MergeVo;
import com.yjc.drygoods.ware.vo.PurchaseDoneVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.yjc.drygoods.ware.entity.ExchangeEntity;
import com.yjc.drygoods.ware.service.ExchangeService;
import com.yjc.common.utils.PageUtils;
import com.yjc.common.utils.R;


/**
 * 换货单
 *
 * @author yjc
 * @email yjc19979401754@126.com
 * @date 2022-11-01 16:20:12
 */
@Slf4j
@RestController
@RequestMapping("ware/exchange")
public class ExchangeController {
    @Autowired
    private ExchangeService exchangeService;

    /**
     * 获取所有已领取的换货单信息以及下辖的所有换货需求
     */
    @GetMapping("/get/all/haved/exchange")
    public R getAllHavedExchangeWithDetail(@RequestParam Map<String, Object> params) {
        PageUtils list = exchangeService.getAllHavedExchangeWithDetail(params);
        log.info("list:{}", list);
        return R.ok().put("data", list);
    }

    /**
     * 获取所有分配给指定id用户的换货单信息以及下辖的所有换货需求
     */
    @GetMapping("/get/all/assigned/exchange")
    public R getAllAssignedExchangeWithDetail(@RequestParam Map<String, Object> params) {
        PageUtils list = exchangeService.getAllAssignedExchangeWithDetail(params);
        log.info("list:{}", list);
        return R.ok().put("data", list);
    }

    /**
     * /ware/exchange/done
     * 完成换货
     */
    @PostMapping("/done")
    public R doneExchange(@RequestBody ExchangeDoneVo exchangeDoneVo) {
        exchangeService.doneExchange(exchangeDoneVo);
        return R.ok();
    }

    /**
     * /ware/exchange/received
     * 领取换货单
     */
    @PostMapping("/received")
    public R receiveExchange(@RequestBody String ids) {
        log.info("ids:{}", ids);
        JSONObject msgEncap = JSON.parseObject(ids);
        List<Long> msgList = JSON.parseArray(msgEncap.getString("ids"), Long.class);
        log.info("ids:{}", msgList);
        exchangeService.receiveExchange(msgList);
        return R.ok();
    }

    /**
     * /ware/exchange/unreceive/list
     * 查询未领取的换货单
     */
    @GetMapping("/unreceive/list")
    public R getUnreceiveList(@RequestParam Map<String, Object> params) {
        PageUtils page = exchangeService.queryPageUnreceiveList(params);
        return R.ok().put("page", page);
    }

    /**
     * /ware/exchange/merge
     * 合并换货需求
     */
    @PostMapping("/merge")
    public R saveMerge(@RequestBody ExMergeVo exMergeVo) {
        exchangeService.saveMerge(exMergeVo);
        return R.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = exchangeService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        ExchangeEntity exchange = exchangeService.getById(id);

        return R.ok().put("exchange", exchange);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody ExchangeEntity exchange) {
        exchange.setCreateTime(new Date());
        exchange.setUpdateTime(new Date());
        exchangeService.save(exchange);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody ExchangeEntity exchange) {
        exchangeService.updateById(exchange);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        exchangeService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
